package com.lzx.adx.report.support.strategy.v2.channel;

import cn.hutool.core.util.URLUtil;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
@ProductType(ProductChannelConstant.HUI_HUANG)
public class HuiHuangReportStrategy extends AbstractReport{
    @Override
    public String getReportFinalUrl(ReportDTO reportDTO, AccountDTO accountDTO) {
        Map<String, Object> prams = new HashMap<>();
        prams.put("os", "0");
        prams.put("ip", reportDTO.getIp());
        prams.put("tms", reportDTO.getTraceTime());
        if (StringUtils.isNotBlank(reportDTO.getUser_agent())) {
            String ua = URLEncoderDecoderTool.decodeURL(reportDTO.getUser_agent());
            prams.put("adAgent", URLEncoderDecoderTool.encodeURL(ua));
        }

        if(StringUtils.isNotBlank(reportDTO.getImeiSum()) &&!StringUtils.contains(reportDTO.getImeiSum(),"__")){
            prams.put("imeiMd5", reportDTO.getImeiSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaid()) &&!StringUtils.contains(reportDTO.getOaid(),"__")){
            prams.put("oaid", reportDTO.getOaid());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaidSum()) &&!StringUtils.contains(reportDTO.getOaidSum(),"__")){
            prams.put("oaidMd5", reportDTO.getOaidSum());
        }

        String callBackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        prams.put("callbackUrl", URLEncoderDecoderTool.encodeURL(callBackUrl));
        String paramsUrl = URLUtil.buildQuery(prams, StandardCharsets.UTF_8);

        return accountDTO.getDspReportUrl() + "&" + paramsUrl;
    }
}
